package com.neusoft.controller;

import com.neusoft.feign.FoodFeignClient;
import com.neusoft.po.Business;
import com.neusoft.po.BusinessGet;
import com.neusoft.po.CommonResult;
import com.neusoft.po.Food;
import com.neusoft.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Business")
@RefreshScope //开启动态刷新
public class BusinessController {
	@Autowired
	private BusinessService businessService;

	@Autowired
	private FoodFeignClient foodFeignClient;

	/**
	 * @Autowired private RestTemplate restTemplate;
	 *
	 * @Autowired private DiscoveryClient discoveryClient;
	 */

	@GetMapping("/Login")
	public CommonResult<BusinessGet> getBusinessByIdByPass(Business business) throws Exception {
		return new CommonResult<BusinessGet>(200, "success", businessService.getBusinessByIdByPass(business));
	}

	@GetMapping("/OrderTypeId")
	public CommonResult<List> listBusinessByOrderTypeId(Business business) throws Exception {
		return new CommonResult<List>(200, "success",
				businessService.listBusinessByOrderTypeId(business.getOrderTypeId()));
	}

	@GetMapping("/BusinessId")
	public CommonResult<BusinessGet> getBusinessById(Business business) throws Exception {

		// 通过服务提供者名（food-server）获取Eureka Server上的元数据
		// List<ServiceInstance> instanceList =
		// discoveryClient.getInstances("food-server");
		// 现在，元数据集合中只有一个服务信息（food-server）
		// ServiceInstance instance = instanceList.get(0);

		BusinessGet business1 = businessService.getBusinessById(business.getBusinessId());
		// 在商家微服务中调用食品微服务
		// Food food = new Food();
		// food.setBusinessId(business.getBusinessId());

		// 在商家微服务中调用食品微服务
		CommonResult<List> result = foodFeignClient.listFoodByBusinessId(business.getBusinessId());

		if (result.getCode() == 200) {
			business1.setFoodList(result.getResult());
		} else {
			business.setFoodList(new ArrayList());
		}

		return new CommonResult(200, "success", business1);
	}

	@PostMapping("/BusinessId")
	public CommonResult<Integer> saveBusiness(Business business) throws Exception {
		return new CommonResult<Integer>(200, "success", businessService.saveBusiness(business));
	}

	@PutMapping("/BusinessId")
	public CommonResult<Integer> updateBusiness(Business business) throws Exception {
		return new CommonResult<Integer>(200, "success", businessService.updateBusiness(business));
	}

	@DeleteMapping("/BusinessId")
	public CommonResult<Integer> removeBusiness(Business business) throws Exception {
		return new CommonResult<Integer>(200, "success", businessService.removeBusiness(business.getBusinessId()));
	}

	@GetMapping("/NewId")
	public CommonResult<Integer> getNewIds() throws Exception {
		return new CommonResult<Integer>(200, "success", businessService.getNewIds());
	}

}
